﻿using AutoMapper;
using BW.Demo.Domain.Entities;
using BW.Demo.ErrorCode;
using BW.Demo.Infrastructure.Interfcaes;
using BW.Demo.WebApi.Applications.Commands;
using MediatR;

namespace BW.Demo.WebApi.Applications.CommandHandlers
{
    public class TraceabilityCreateCommandHandler : IRequestHandler<TraceabilityCreateCommand, APIResult<int>>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRespitory<TraceabilityManagement> _baseRespitory;
        public TraceabilityCreateCommandHandler(IMapper mapper, IBaseRespitory<TraceabilityManagement> baseRespitory)
        {
            _mapper = mapper;
            _baseRespitory = baseRespitory;
        }
        public async Task<APIResult<int>> Handle(TraceabilityCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            TraceabilityManagement trace = _mapper.Map<TraceabilityManagement>(request);
            int num = await _baseRespitory.CreateAsync(trace);
            trace.CreateTime = DateTime.Now;
            if (num > 0)
            {
                result.Code=ResultCode.添加成功;
                result.Message=ResultCode.添加成功.ToString();
                result.Data = num;
                
            }
            else
            {
                
                result.Code = ResultCode.添加成功;
                result.Message = ResultCode.添加失败.ToString();
                result.Data = num;
            }

            return await Task.FromResult(result);
        }
    }
}
